package com.coin.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coin.domain.SysPrivilege;
import com.coin.model.R;
import com.coin.service.SysPrivilegeService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @Auther: 李 力
 * @Date: 2025/5/26
 * @Description: 权限管理
 * @version: 1.0
 */
@Api(tags = "权限管理")
@RestController
@RequestMapping("/privileges")
public class SysPrivilegeController {

    @Autowired
    private SysPrivilegeService privilegeService;

    /**
     * @Description: 分页查询
     * @Author: 李 力
     * @Date: 2025/5/26
     * @Param: current size
     * @return: R
     */
    @GetMapping
    @PreAuthorize("hasAuthority('sys_privilege_query')")
    @ApiOperation(value = "查询角色的权限列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页大小")

    })
    public R<Page<SysPrivilege>> findPage(Page<SysPrivilege> page) {
        page.addOrder(OrderItem.desc("last_update_time"));
        Page<SysPrivilege> sysPrivilegePage = privilegeService.page(page);
        return R.ok(sysPrivilegePage);
    }

    /**
     * @Description: 新增权限
     * @Author: 李 力
     * @Date: 2025/5/26
     * @Param: sysPrivilege
     * @return: R
     */
    @PostMapping
    @ApiOperation(value = "新增一个权限")
    @PreAuthorize("hasAuthority('sys_privilege_create')")
    public R add(@RequestBody SysPrivilege sysPrivilege) {
        boolean save = privilegeService.save(sysPrivilege);
        return save ? R.ok("新增成功") : R.fail("新增失败");
    }

    /**
     * @Description: 修改权限
     * @Author: 李 力
     * @Date: 2025/5/26
     * @Param: sysPrivilege
     * @return: R
     */
    @PutMapping
    @ApiOperation(value = "修改一个权限")
    @PreAuthorize("hasAuthority('sys_privilege_update')")
    public R update(@RequestBody SysPrivilege sysPrivilege) {
        boolean save = privilegeService.updateById(sysPrivilege);
        return save ? R.ok("修改成功") : R.fail("修改失败");
    }

}
